<?php

class ocorrencia_cacheModel extends Model{
    public $tabela = "ocorrencia_cache";
    public $pkey   = array('cod_cache');
    public $dados  = array(
        'cod_cache' => array(
	    'name'     => 'Código',
	    'type'     => 'int',
            'ai'       => true,
	    'size'     => '11',
	    'pkey'    => true,
	    'notnull' => true,
         ),
         'cod_usuario' => array(
	    'name'     => 'Usuário',
	    'type'     => 'int',
	    'size'     => '11',
	    'fkey' => array(
	        'model' => 'usuario/login',
	        'cardinalidade' => '1n',
	        'keys' => array('cod_usuario', 'user_name'),
                'onupdate' => 'cascade',
                'ondelete' => 'cascade',
	    ),
        ),
        'data' => array(
	    'name'    => 'Cache',
	    'type'    => 'timestamp',
	    'notnull' => true,
	    'grid'    => true,
	    'display' => true,
        ),
        'cachelabel' => array(
            'name'    => 'Nome',
	    'type'    => 'varchar',
            'size'    => '64'
        ),
        'cache' => array(
            'name'    => 'Cache',
	    'type'    => 'text',
        ),
        /*'cache_cat' => array(
            'name'    => 'Categoria',
	    'type'    => 'varchar',
            'size'    => '32'
        )*/
        
   );
    public function __construct() {
        parent::__construct();
        $this->LoadModel('usuario/login', 'uobj');
    }
    
    public function setCache($label, $cache, $saveuser = true){
        if($saveuser) $arr['cod_usuario'] = $this->uobj->getCodUsuario();
        $arr['cachelabel'] = $label;
        $arr['cache']      = $cache;
        return parent::inserir($arr);
    }
    
    /*public function setCache($label, $cache, $categoria, $saveuser = true){
        if($saveuser) $arr['cod_usuario'] = $this->uobj->getCodUsuario();
        $arr['cachelabel'] = $label;
        $arr['cache']      = $cache;
        $arr['cache_cat']  = $categoria;
        return parent::inserir($arr);
    }*/
    
    public function getCache($label, $saveuser = true){
        $where = " cachelabel = '$label' ";
        if($saveuser){
            $cod_usuario = $this->uobj->getCodUsuario();
            $where .= " AND cod_usuario = '$cod_usuario'";
        }
        $var = parent::selecionar(array('cache'), $where);
        if(empty ($var)) return "";
        $var = array_shift($var);
        return $var['cache'];
    }
    
    public function clear($label){
        return parent::apagar($label, 'cachelabel');
    }
    
    public function clearUserCache(){
        $cod_usuario = $this->uobj->getCodUsuario();
        return parent::apagar($cod_usuario, 'cod_usuario');
    }
}

?>
